/** ---------------------公共函数类----------------------- */

// 获取当前日期
function getDay() {
	let today = new Date();
	let year = today.getFullYear();
	let month = today.getMonth() + 1;
	let day = today.getDate();
	// 在月份和日期小于 10 的情况下补零
	if (month < 10) {
		month = '0' + month;
	}
	if (day < 10) {
		day = '0' + day;
	}
	return year + '-' + month + '-' + day;
}


// 获取当前日期 yyyy-mm-dd HH:mm:ss
function getCurrentDateTime() {
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    let day = now.getDate();
    let hours = now.getHours();
    let minutes = now.getMinutes();
    let seconds = now.getSeconds();

    // 添加前导零
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    hours = hours < 10 ? '0' + hours : hours;
    minutes = minutes < 10 ? '0' + minutes : minutes;
    seconds = seconds < 10 ? '0' + seconds : seconds;

    let dateTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    return dateTime;
}


function formatDate(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    // 添加前导零
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;

    return year + '-' + month + '-' + day;
}
// 根据xx 获取对应的日期
function geBeforetDay(day) {
    var today = new Date();
    var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetday_milliseconds);

    return formatDate(today);
}


/** 
 * @function 计算两个日期相差的天数
 * @param {date} dateString1 减数
 * @param {date} dateString2 被减数
 */
function getDaysBetween(dateString1, dateString2) {
    let startDate = Date.parse(dateString1);
    let endDate = Date.parse(dateString2);
	console.log('计算两个日期相差的天数')
    return (startDate - endDate) / (1 * 24 * 60 * 60 * 1000);
}

/**
 * @function 处理日期格式
 */
const formatTime = (time, format) => {
    console.log('time', time);
    const date = new Date(time);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    
    format = format.replace('YYYY', year);
    format = format.replace('MM', month);
    format = format.replace('DD', day);
    format = format.replace('HH', hours);
    format = format.replace('mm', minutes);
    format = format.replace('ss', seconds);
    return format;
  }
  
/**
* @function 将数字格式转换为#,###
* @param {object} number 需要转换的内容
*/
function textFormat(number) {
	// 将数字格式化为#,###的形式
	 return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

// 任务类型数据
const taskTypeData = [
	"全部",
	"首保招揽",
	"首付招揽",
	"定保招揽",
	"保修到期",
	"流失预警",
	"续期招揽",
	"活动招揽",
	"其他招揽",
]

export default {
	textFormat,
	getDay,
	getDaysBetween,
	taskTypeData,
	formatTime,
	formatDate,
	geBeforetDay,
	getCurrentDateTime,
}
